﻿using AutoMapper;
using JuCheap.Core.Infrastructure;
using JuCheap.Core.Infrastructure.Utilities;
using JuCheap.Core.IService;
using JuCheap.Core.Model;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace JuCheap.Core.WebApi.Controllers
{
    /// <summary>
    /// 部门管理api
    /// </summary>
    [Route("api/[controller]")]
    public class DepartmentController : BaseApiController
    {
        private readonly ISysDepartmentService _sysDepartmentService;
        private readonly IDepartmentService _departmentService;
        private readonly IMapper _mapper;

        /// <summary>
        /// ctor
        /// </summary>
        public DepartmentController(ISysDepartmentService sysDepartmentService,
            IDepartmentService departmentService,
            IMapper mapper)
        {
            _sysDepartmentService = sysDepartmentService;
            _departmentService = departmentService;
            _mapper = mapper;
        }

        /// <summary>
        /// 创建部门信息
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("create")]
        public async Task<ActionResult<string>> Create([FromBody]DepartmentCreateRequestDto req)
        {
            var department = _mapper.Map<SysDepartmentDto>(req);
            var departmentId = await _sysDepartmentService.InsertAsync(department);
            return Ok(departmentId);
        }

        /// <summary>
        /// 分页获取部门信息
        /// </summary>
        [HttpPost]
        [Route("search")]
        public async Task<ActionResult<PagedResult<SysDepartmentDto>>> Search([FromBody]SysDepartmentFilter filter)
        {
            var departments = await _departmentService.GetTableTreeAsync(filter);
            var records = new PagedResult<SysDepartmentDto>()
            {
                PageIndex = filter.PageIndex,
                PageSize = filter.PageSize,
                Datas = departments,
                Records = departments.Count()
            };
            return Ok(records);
        }

        /// <summary>
        /// 获取部门信息
        /// </summary>
        [HttpGet]
        [Route("{id}")]
        public async Task<ActionResult<SysMenuDto>> Get([FromRoute]string id)
        {
            var menuDto = await _sysDepartmentService.FindAsync(id);
            return Ok(menuDto);
        }

        /// <summary>
        /// 修改部门信息
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("edit")]
        public async Task<ActionResult<bool>> Edit([FromBody]DepartmentEditRequestDto req)
        {
            var department = _mapper.Map<SysDepartmentDto>(req);
            await _sysDepartmentService.UpdateAsync(department);
            return Ok(true);
        }

        /// <summary>
        /// 批量删除部门信息
        /// </summary>
        /// <param name="ids">id集合</param>
        /// <returns></returns>
        [HttpPost]
        [Route("delete")]
        public async Task<ActionResult<bool>> Delete([FromBody]IList<string> ids)
        {
            await _sysDepartmentService.DeleteAsync(ids);
            return Ok(true);
        }

        /// <summary>
        /// 获取部门树状结构
        /// </summary>
        [HttpGet]
        [Route("select-tree")]
        public async Task<ActionResult<List<SelectTreeResponseDto>>> GetTree()
        {
            var treeList = await _departmentService.GetSimpleTreeAsync();
            return Ok(treeList);
        }
    }
}